package liuliu.throughwaring.ui.activity;

import android.app.Dialog;
import android.view.View;
import android.widget.Button;

import net.tsz.afinal.annotation.view.CodeNote;

import liuliu.custom.control.edittext.ImageEditText;
import liuliu.custom.control.toolbar.TToolbar;
import liuliu.custom.method.Utils;
import liuliu.throughwaring.R;
import liuliu.throughwaring.base.BaseActivity;
import liuliu.throughwaring.control.main.IMainView;
import liuliu.throughwaring.control.main.MainListener;
import liuliu.throughwaring.control.settting.ISettingView;
import liuliu.throughwaring.control.settting.SettingListener;
import liuliu.throughwaring.model.CodeModel;
import liuliu.throughwaring.model.CompanyModel;
import liuliu.throughwaring.model.Key;
import liuliu.throughwaring.model.ModifiedModel;
import liuliu.throughwaring.model.NoticeModel;
import liuliu.throughwaring.model.PersonModel;
import liuliu.throughwaring.model.PoliceModel;
import liuliu.throughwaring.model.TerminalModel;

/**
 * Created by liuliu on 2015/11/13   14:41
 * <p>
 * 网络设置
 *
 * @author 柳伟杰
 * @Email 1031066280@qq.com
 */
public class SettingActivity extends BaseActivity {
    public static SettingActivity mInstance;
    @CodeNote(id = R.id.setting_ip)
    ImageEditText setting_ip;
    @CodeNote(id = R.id.setting_port)
    ImageEditText setting_port;
    @CodeNote(id = R.id.setting_http_submit, click = "onClick")
    Button submit;
    @CodeNote(id = R.id.setting_toolbar)
    TToolbar mToolbar;

String isSetting="";
    @Override
    public void initViews() {
        setContentView(R.layout.activity_setting);
        mInstance = this;
        isSetting=getIntent().getStringExtra("Setting");
    }

    @Override
    public void initEvents() {
        try {
            if(isSetting.equals("1")){
                setting_ip.setText(Utils.ReadString(Key.KEY_IP));
                setting_port.setText(Utils.ReadString(Key.KEY_PORT));
            }else {
                if (!Utils.ReadString(Key.KEY_IP).equals("") && !Utils.ReadString(Key.KEY_PORT).equals("")) {
                    mUtils.IntentPost(LoadingActivity.class);
                    mInstance.finish();
                } else {
                    setting_ip.setText(Utils.ReadString(Key.KEY_IP));
                    setting_port.setText(Utils.ReadString(Key.KEY_PORT));
                }
            }
        } catch (Exception e) {
            mUtils.IntentPost(LoadingActivity.class);
            mInstance.finish();
        }
        mToolbar.setLeftOnClick(new TToolbar.LeftOnClickListener() {
            @Override
            public void leftclick() {
                mInstance.finish();
            }
        });
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.setting_http_submit:
                String ip = setting_ip.getText().trim();
                String port = setting_port.getText().trim();
                if (ip.equals("") || port.equals("")) {
                    mUtils.ToastShort("ip或者端口不能为空！");
                } else {
                    if (Utils.ReadString(Key.KEY_PORT).equals("")) {
                        mUtils.WriteString(Key.KEY_IP, ip);
                        mUtils.WriteString(Key.KEY_PORT, port);
                        mUtils.IntentPost(LoadingActivity.class);
                        mInstance.finish();
                    } else {
                        mUtils.WriteString(Key.KEY_IP, ip);
                        mUtils.WriteString(Key.KEY_PORT, port);
                        mInstance.finish();
                    }
                }
                break;
        }
    }

}
